﻿Option Strict On

Imports KUeLib.Vertraege
Imports KUeLib.Attachments
Imports KUeLib.Data
Imports KUeLib.Data.ServiceDefinitions
Imports ExHandlerLib
Imports KUeLib.Buchungen
Imports System.Windows.Forms

Namespace Formulare.Vertraege

    Public Class VertragForm
        Inherits WeifenLuo.WinFormsUI.Docking.DockContent
        Implements LibMultiPlug.Core.Reporting.IReportable

        Private Const PROCESS_NAME As String = "vertraege"

        Private _vertrag As Vertrag
        Private _vertragsService As VertragsDataService = _
                                        ServiceManager.GetInstance().GetVertragDataService()

        Private _buchungen As List(Of Buchung)
        Private _buchungenService As BuchungenDataSerivce = _
                                        ServiceManager.GetInstance().GetBuchungenDataService()

        Private _attachments As New List(Of KUeLib.Attachments.FileAttachment)
        Private _fileAttachmentService As FileAttachmentDataService = _
                                        ServiceManager.GetInstance().GetFileAttachmentDataService()

        Private _statusService As StatusDataService = _
                                        ServiceManager.GetInstance().GetStatusDataService()

        Private _errorManager As ErrorProviderManager

        Public Sub New()

            ' Dieser Aufruf ist für den Designer erforderlich.
            InitializeComponent()

            ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

            'CoorpDesignLib.ControlFormater.GetInstance().RegisterControls(Me.Controls)

            _errorManager = New ErrorProviderManager

        End Sub

        Public Overloads Sub Show(ByVal dockPanel As WeifenLuo.WinFormsUI.Docking.DockPanel, ByVal vertragId As Integer) Implements LibMultiPlug.Core.Reporting.IReportable.Show
            _vertrag = _vertragsService.GetVertragById(vertragId)
            Call InitializeForm()
            MyBase.Show(dockPanel)
        End Sub

        Public Overloads Sub Show(ByVal dockPanel As WeifenLuo.WinFormsUI.Docking.DockPanel)
            _vertrag = New Vertrag
            Call InitializeForm()
            MyBase.Show(dockPanel)
        End Sub

        Private Sub ReleaseBindings(ByVal cc As System.Windows.Forms.Control.ControlCollection, _
                                    Optional ByVal recursive As Boolean = True)
            For Each c As Control In cc
                c.DataBindings.Clear()
                If recursive AndAlso c.Controls.Count > 0 Then _
                    ReleaseBindings(c.Controls)
            Next
        End Sub

        Private Sub InitializeForm()

            cbStatus.DataSource = _statusService.GetStatusForProcess(PROCESS_NAME)
            cbStatus.DisplayMember = "Bezeichnung"
            cbStatus.ValueMember = "Bezeichnung"

            Call BindContents()
        End Sub

        Private Sub BindContents()

            _errorManager = New ErrorProviderManager

            ReleaseBindings(Me.Controls)

            Me.Text = String.Format("Vertrag #{0}", _vertrag.Id.ToString("000000"))

            lblHeader.Text = String.Format("Vertrag #{0}", _vertrag.Id.ToString("000000"))

            tbBezeichnung.DataBindings.Add("Text", _vertrag, "Bezeichnung")
            _errorManager.RegisterControl(tbBezeichnung, "Text")

            tbVertragspartner.DataBindings.Add("Text", _vertrag, "Partner")
            _errorManager.RegisterControl(tbVertragspartner, "Text")

            tbVertragspartnerDetails.DataBindings.Add("Text", _vertrag, "PartnerDetails")
            _errorManager.RegisterControl(tbVertragspartnerDetails, "Text")

            cbWiedervorlage.DataBindings.Add("Checked", _vertrag, "Wiedervorlage")
            _errorManager.RegisterControl(cbWiedervorlage, "Checked")

            dtpWiedervorlage.DataBindings.Add("Value", _vertrag, "WiedervorlageDatum")
            _errorManager.RegisterControl(dtpWiedervorlage, "Value")

            dtpKuendigungsdatum.DataBindings.Add("Value", _vertrag, "Kuendigungsdatum")
            _errorManager.RegisterControl(dtpKuendigungsdatum, "Value")

            dtpVertragsabschluss.DataBindings.Add("Value", _vertrag, "Abschlussdatum")
            _errorManager.RegisterControl(dtpVertragsabschluss, "Value")

            dtpVertragsende.DataBindings.Add("Value", _vertrag, "Ablaufdatum")
            _errorManager.RegisterControl(dtpVertragsende, "Value")

            tbZahlungsintervall.DataBindings.Add("Text", _vertrag, "Zahlungsintervall")
            _errorManager.RegisterControl(tbZahlungsintervall, "Text")

            tbBetrag.DataBindings.Add("Text", _vertrag, "Betrag")
            _errorManager.RegisterControl(tbBetrag, "Text")

            RemoveHandler tbBetrag.DataBindings("Text").Format, AddressOf CoorpDesignLib.ControlFormater.FormatCurrencyEventHandler
            AddHandler tbBetrag.DataBindings("Text").Format, AddressOf CoorpDesignLib.ControlFormater.FormatCurrencyEventHandler

            cbStatus.DataBindings.Add("SelectedValue", _vertrag, "Status")
            _errorManager.RegisterControl(cbStatus, "SelectedValue")

            tbInhalt.DataBindings.Add("Text", _vertrag, "Inhalt")
            _errorManager.RegisterControl(tbInhalt, "Text")

        End Sub

        Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
            _vertrag.Id = _vertragsService.Save(_vertrag)
            BindContents()
        End Sub

        Private Sub TabPage2_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabPage2.Enter
            Call InitializeBuchungen()
        End Sub

        Private Sub InitializeBuchungen()
            _buchungen = _buchungenService.GetBuchungenForVertragList(_vertrag.Id)

            lvBuchungen.Items.Clear()

            For Each b As Buchung In _buchungen
                Dim lvi As ListViewItem = lvBuchungen.Items.Add(b.Buchungsdatum.ToShortDateString())
                lvi.SubItems.Add(b.Betrag.ToString("C", (New System.Globalization.CultureInfo("de-DE")).NumberFormat))
                lvi.SubItems.Add(b.Status)
                lvi.SubItems.Add(b.Bemerkung)
            Next

        End Sub

        Private Sub lvBuchungen_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvBuchungen.MouseDoubleClick
            Dim bdf As New BuchungDetailForm()
            bdf.Show(Me.DockPanel, _buchungen(lvBuchungen.SelectedIndices(0)).Id)
        End Sub

        Private Sub InitializeFiles()

            _attachments = _fileAttachmentService.GetAttachmentForProcess(_vertrag.Id)
            lvAttachments.Items.Clear()

            For Each fa As FileAttachment In _attachments
                Dim lvi As ListViewItem = lvAttachments.Items.Add(fa.Bezeichnung)
                lvi.SubItems.Add(fa.Filename)
                lvi.SubItems.Add(fa.Datum.ToShortDateString())
            Next

        End Sub

        Private Sub btnNewFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewFile.Click
            Dim faf As New Formulare.Fileattachments.FileAttachmentForm
            faf.ShowDialog(_vertrag.Id)
            InitializeFiles()
        End Sub

        Private Sub btnOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenFile.Click
            If lvAttachments.SelectedIndices.Count > 0 Then
                _fileAttachmentService.FillBinary(_attachments(lvAttachments.SelectedIndices(0)))
                _attachments(lvAttachments.SelectedIndices(0)).StartAsProcess()
            End If

        End Sub

        Private Sub TabPage3_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabPage3.Enter
            InitializeFiles()
        End Sub

        Private Sub btnDeleteFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteFile.Click
            If lvAttachments.SelectedItems.Count > 0 Then
                If MessageBox.Show(String.Format("Sind Sie sicher, dass Sie den Anhang '{0}' löschen möchten?", _attachments(lvAttachments.SelectedIndices(0)).Bezeichnung), _
                                   "Sicherheitsabfrage", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    _fileAttachmentService.Delete(_attachments(lvAttachments.SelectedIndices(0)).Id)
                    Call InitializeFiles()
                End If
            End If
        End Sub

    End Class

End Namespace